(function (vc) {
    vc.extends({
        data: {
            registerInfo: {
                shopName: '',
                tel: '',
                passwd: '',
                logo: '',
                areas: [],
                provs: [],
                citys: [],
                selectProv: '',
                selectCity: '',
                selectArea: '',
                areaAddress: '',
                areaCode: '',
                shopType: '',
                shopTypes: [],
                messageCode: '',
                buttonInfo: '验证码'
            }
        },
        _initMethod: function () {
            $that._initSysInfo();
        },
        _initEvent: function () {
            $that._initArea('101', '0');
            $that._listShopTypes();
        },
        methods: {
            _initSysInfo: function () {
                var sysInfo = vc.getData("_sysInfo");
                if (sysInfo == null) {
                    //this.logo = "HC";
                    $that._loadSysInfo();
                    return;
                }
                this.logo = sysInfo.logo;
            },
            _loadSysInfo: function () {
                var param = {
                    params: {
                        sys: 'HC'
                    }
                }
                vc.http.apiGet(
                    '/login.getSysInfo',
                    param,
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status != 200) {
                            console.log("加载系统信息失败");
                            vc.saveData("_sysInfo", { logo: 'HC' });
                            vc.copyObject(json, $that.registerInfo);
                            return;
                        }
                        vc.copyObject(JSON.parse(json), $that.registerInfo);
                        //保存到浏览器
                        vc.saveData("_sysInfo", JSON.parse(json));
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.saveData("_sysInfo", { logo: 'HC' });
                        vc.copyObject(json, $that.registerInfo);
                        $that.loginInfo.errorInfo = errInfo;
                    });
            },
            validate: function () {
                return vc.validate.validate({
                    registerInfo: $that.registerInfo
                }, {
                    'registerInfo.shopName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺名不能为空"
                        }
                    ],
                    'registerInfo.passwd': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "密码不能为空"
                        },
                    ],
                    'registerInfo.tel': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "手机号不能为空"
                        }
                    ],
                    'registerInfo.messageCode': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "验证码不能为空"
                        }
                    ],
                    'registerInfo.areaCode': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "地区不能为空"
                        }
                    ],

                });
            },
            doRegister: function () {
                if (!$that.validate()) {
                    vc.toast(vc.validate.errInfo)
                    return;
                }
                let _obj = $that.registerInfo;
                let _data = {
                    shopName: _obj.shopName,
                    link: _obj.tel,
                    password: _obj.passwd,
                    areaAddress: _obj.areaAddress,
                    areaCode: _obj.areaCode,
                    shopType: _obj.shopType,
                    msgCode: _obj.messageCode,
                }
                vc.http.apiPost(
                    '/shop.registerStoreAndShop',
                    JSON.stringify(_data),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json)
                        if (_json.code == 0) {
                            vc.jumpToPage("/");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });

            },
            getProv: function (_prov) {
                $that._initArea('202', _prov);
            },
            getCity: function (_city) {
                $that._initArea('303', _city);
            },
            getArea: function (_area) {
                $that.registerInfo.areaAddress = '';
                $that.registerInfo.areaCode = _area;
                if (!$that.registerInfo.provs) {
                    return;
                }
                $that.registerInfo.provs.forEach(function (_param) {
                    if (_param.areaCode == $that.registerInfo.selectProv) {
                        $that.registerInfo.areaAddress = _param.areaName;
                    }
                });

                $that.registerInfo.citys.forEach(function (_param) {
                    if (_param.areaCode == $that.registerInfo.selectCity) {
                        $that.registerInfo.areaAddress += _param.areaName;
                    }
                });
                $that.registerInfo.areas.forEach(function (_param) {
                    if (_param.areaCode == $that.registerInfo.selectArea) {
                        $that.registerInfo.areaAddress += _param.areaName;
                    }
                });

            },
            _initArea: function (_areaLevel, _parentAreaCode) { //加载区域
                var _param = {
                    params: {
                        areaLevel: _areaLevel,
                        parentAreaCode: _parentAreaCode
                    }
                };
                vc.http.apiGet('/area.listAreas',
                    _param,
                    function (json, res) {
                        if (res.status == 200) {
                            let _tmpAreas = JSON.parse(json).data;
                            if (_areaLevel == '101') {
                                $that.registerInfo.provs = _tmpAreas;
                            } else if (_areaLevel == '202') {
                                $that.registerInfo.citys = _tmpAreas;
                            } else {
                                $that.registerInfo.areas = _tmpAreas;
                            }
                            return;
                        }
                        //$that.$emit('errorInfoEvent',json);
                    }, function (errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                        vc.toast("查询地区失败");
                    });
            },
            _listShopTypes: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 100,
                    }
                };
                //发送get请求
                vc.http.apiGet('/shopType.queryShopType',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.registerInfo.shopTypes = _json.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            sendTelMessageCode: function () {
                if (!$that.registerInfo.tel) {
                    vc.toast("手机号码为空或不正确，不能发送验证码");
                    return;
                }
                if ($that.registerInfo.buttonInfo != '验证码') {
                    return;
                }
                // 开启定时
                $that.messageCodeTimer();
                let param = {
                    tel: $that.registerInfo.tel,
                };
                vc.http.apiPost('/user.userSendSms',
                    JSON.stringify(param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast(_json.data);
                            return;
                        }
                    }, function (errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                    });

            },
            //验证码定时
            messageCodeTimer() {
                let num = 60;
                let _timer = vc.createTimer(function () {
                    num--;
                    $that.registerInfo.buttonInfo = num + " 秒后重试";
                    if (num === 1) {
                        vc.clearTimer(_timer);
                        $that.registerInfo.buttonInfo = "验证码";
                    }
                }, 1000);
            },
            _doLogin: function () {
                vc.jumpToPage('/user.html#/pages/frame/login');
            }
        }
    });


})(window.vc);